package com.atguigu.lease.web.app.service.impl;


import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.common.utils.VerifyCodeUtil;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.app.service.LoginService;
import com.atguigu.lease.web.app.service.SmsService;
import com.atguigu.lease.web.app.service.UserInfoService;
import com.atguigu.lease.web.app.vo.user.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    SmsService smsService;
    @Autowired
    UserInfoService userInfoService;
    @Override
    public void getCode(String phone) {
        if (!StringUtils.hasText(phone)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_PHONE_EMPTY);
        }

        //生成验证码
        String code = VerifyCodeUtil.verifyCode(6);

        //保存验证码到redis
        String codeKey = RedisConstant.APP_LOGIN_PREFIX + phone;

        Boolean flag = redisTemplate.hasKey(codeKey);
        if (flag){
            Long expire = redisTemplate.getExpire(codeKey);
            if (RedisConstant.APP_LOGIN_CODE_TTL_SEC - expire < RedisConstant.APP_LOGIN_CODE_RESEND_TIME_SEC)

            throw new LeaseException(ResultCodeEnum.APP_SEND_SMS_TOO_OFTEN);
        }


        redisTemplate.opsForValue().set(codeKey, code, RedisConstant.APP_LOGIN_CODE_TTL_SEC, TimeUnit.SECONDS);

        //发送验证码到手机
        smsService.sendCode(phone, code);

    }

    @Override
    public String login(LoginVo loginVo) {

        String phone = loginVo.getPhone();
        String code = loginVo.getCode();

        //判断手机号和验证码是否为空
        if (!StringUtils.hasText(phone)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_PHONE_EMPTY);
        }

        if (!StringUtils.hasText(code)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
        }

        //获取redis中的验证码
        String codeKey = RedisConstant.APP_LOGIN_PREFIX + phone;
        Object redisCode = redisTemplate.opsForValue().get(codeKey);
        if (redisCode == null){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EXPIRED);
        }

        //redis验证码和输入验证码进行比较
        if (!code.equals(redisCode)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_ERROR);
        }

        //根据手机号查询数据库信息
        UserInfo userDb = userInfoService.getOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getPhone, phone));
        if (userDb == null){
            //说明用户第一次使用，主动把用户添加为会员
            userDb = new UserInfo();
            userDb.setPhone(phone);
            userDb.setNickname("用户-" + phone.substring(7));
            userDb.setStatus(BaseStatus.ENABLE);
            userInfoService.save(userDb);
        }

        //创建token
        String token = JwtUtil.createToken(phone, userDb.getId());

        return token;
    }
}
